<?php
// event/Form/Event.php

class Form_TLeg extends Zend_Form
{

    protected $_noDecorator = array(
        'ViewHelper', 
        'Errors'
    );

    public function cluBuild($params)
    {
    	$this->setMethod('POST');
        if($params['action'] == 'new')
        {
//        	$a = $params['view']->hvnUrl(array('module' => 'tour', 'action'=>'new-tleg')) .
//        				 "?tid=" . $params['tid'] . "&leg_number=".$params['leg_number'];
//        	$this->setAction($a);
            $this->setAttrib('id', 'new_tleg');
        } 
		elseif($params['action'] == 'edit')
		{
            $this->setAttrib('id', 'edit_tleg');
//        	$a = $params['view']->hvnUrl(array('module' => 'tour', 'action'=>'edit-tleg')) . "?id=".$params['id'];
//        	$this->setAction($a);
		}
		
		$this->setAction($params['action_url']);
    	
    	$filterHtmlBody = new Clu_Filter_HtmlBody();
		$myValidator = new Zend_Validate_Regex('/^[A-Z0-9_\s\-]*$/i'); //alpha, num, space, -, _
        $myValidator->setMessage(t(60), Zend_Validate_Regex::NOT_MATCH);
		$this->addElementPrefixPath('Clu_Filter', 'Clu/Filter/', 'filter');
		
		//tleg name

        //transport company ("vietnam airlines", "Hoang Long Bus"...)
        $tleg_name = new Zend_Form_Element_Text('tleg_name',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $tleg_name->addDecorators($this->_noDecorator);
        $tleg_name->size = 20; // TODO
        $this->addElement($tleg_name);
        		
        //add language
        $transport_type = new Zend_Form_Element_Select('transport_type',  
                         array('disableLoadDefaultDecorators' => true,
                                'required' => true
                         )); 
        $transport_type->addMultiOption('0',t(138))
        			   ->addMultiOption('car',t(703))
                       ->addMultiOption('airplane',t(704))
                       ->addMultiOption('train',t(705))
                       ->addMultiOption('tram',t(706))
                       ->addMultiOption('ship',t(707))
                       ->addMultiOption('motorbike',t(708))
                       ->addMultiOption('bike',t(709))
                       ->addMultiOption('walk',t(710))
                       ->addMultiOption('other',t(711))
                       ->addDecorators($this->_noDecorator);
        $this->addElement($transport_type);


        //transport company ("vietnam airlines", "Hoang Long Bus"...)
        $transport_company = new Zend_Form_Element_Text('transport_company',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $transport_company->addDecorators($this->_noDecorator);
        $departure_date->size = 20; // TODO
        $this->addElement($transport_company);
        
        
        //departure place
        $departure_place_name = new Zend_Form_Element_Text('departure_place_name', 
                         array('disableLoadDefaultDecorators' => true,
                               /* 'required'   => true, */             
                               'class' => 'inputText'));
        
        $departure_place_name->addDecorators($this->_noDecorator);
        	 //->addValidator($myValidator);
        $this->addElement($departure_place_name);
        
        //arrival place
        $arrival_place_name = new Zend_Form_Element_Text('arrival_place_name', 
                         array('disableLoadDefaultDecorators' => true,
                               /* 'required'   => true, */             
                               'class' => 'inputText'));
        
        $arrival_place_name->addDecorators($this->_noDecorator);
        	 //->addValidator($myValidator);
        $this->addElement($arrival_place_name);
        
        //accommodation type of the arrival place
        $arrival_accommodation_type = new Zend_Form_Element_Select('arrival_accommodation_type',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
        $arrival_accommodation_type->addDecorators($this->_noDecorator);
      	$arrival_accommodation_type->addMultiOption('0',t(138))
      							   ->addMultiOption('hotel5',t(712))
      							   ->addMultiOption('hotel4',t(713))
      							   ->addMultiOption('hotel3',t(714))
      							   ->addMultiOption('guesthouse',t(715))
      							   ->addMultiOption('other',t(100));
		if ($params['action'] == 'new')      							   
       		$arrival_accommodation_type->setValue('0');
       		
        $this->addElement($arrival_accommodation_type);
        
        
        //is_last leg checkbox
        $is_last_leg = new Zend_Form_Element_Checkbox('is_last_leg', 
                         array('disableLoadDefaultDecorators' => true,
                               /* 'required'   => true, */             
                               'class' => 'inputText'));
        
        $is_last_leg->addDecorators($this->_noDecorator);
        	 //->addValidator($myValidator);
        $this->addElement($is_last_leg);
        
        
		//tour duration: number of hours
        $duration = new Zend_Form_Element_Text('duration', 
                         array('disableLoadDefaultDecorators' => true,
                               /* 'required'   => true, */             
                               'class' => 'inputText'));
        
        $duration->addDecorators($this->_noDecorator)
        	 ->addValidator($myValidator);
        $this->addElement($duration);
        
        //add start hour
        $departure_hour = new Zend_Form_Element_Select('departure_time',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
        $departure_hour->addDecorators($this->_noDecorator);
		for($i = 0; $i <=  23; $i++)
        	$departure_hour->addMultiOption($i,$i);
       	$departure_hour->setValue('8');
        $this->addElement($departure_hour);

        //add finish hour
        $arrival_time = new Zend_Form_Element_Select('arrival_time',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $arrival_time->addDecorators($this->_noDecorator);
		for($i = 0; $i <=  23; $i++)
        	$arrival_time->addMultiOption($i,$i);
        $arrival_time->setValue('20');
        $this->addElement($arrival_time);
        
        //add start date
        $departure_date= new Zend_Form_Element_Text('departure_date',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
         
        $departure_date->addDecorators($this->_noDecorator);
        $departure_date->size = 10; //TODO
        $this->addElement($departure_date);
        
        //add arrival date
        $arrival_date = new Zend_Form_Element_Text('arrival_date',  
                         array('disableLoadDefaultDecorators' => true,
//                                'required' => true
                         )); 
        $arrival_date->size = 10;
        $arrival_date->addDecorators($this->_noDecorator);
        $this->addElement($arrival_date);
        
        if ($params['action'] === 'new' && (strpos ($params['userPermissions'], 'set status tour') === false))
        {
            //add check captcha
            $elementCaptcha = 
            				   new Zend_Form_Element_Captcha('captcha_newtleg',
                               array('captcha' => 'Image','captchaOptions' =>
                               array(
                                    'captcha' => 'Image',
                                    'wordLen' => 4,
                                    'timeout' => 300,
                                    'imgDir'  =>APP_PATH.'/public/captcha',
                                    'imgUrl' => '/public/captcha/',
                                    'width' => 175,
                                    'height' => 60,
                                    'font' => APP_PATH.'/public/captcha/font/tahoma.ttf',
                                    'DotNoiseLevel' => 10,  
                                    'LineNoiseLevel' => 1,
                                    'expiration' => 3,
                                    'gcFreq' => 1))
            );
            $elementCaptcha->setLabel(t(39).':')
                           ->setAttrib('id','captcha_newtlegImage')
                       	   ->setRequired(true);
            $this->addElement($elementCaptcha);
		}    
        
        
        //add description 
        $description = new Clu_Form_Element_FckEditor('description', array(
        		'fckOptions' => array (
        						'Height' => 600,
        						'Width'  => 600,
        						'ToolbarSet' => 'Full'
        					)
       			 )
        );
        $description->setLabel(t(21));
   		if (strpos ($params['userPermissions'], 'set status tour'))
		{
        	$description->addFilter($filterHtmlBody ); 
		}
        $this->addElement($description);
        
        // add the submit button
		if ($params['action'] === 'new')
    		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                           'label' => t(58) ));
      	else
      		$submit = new Zend_Form_Element_Submit('submit', array('disableLoadDefaultDecorators' => true, 
                                                               'label' => t(59) ));
        
        $submit->addDecorators($this->_noDecorator);
        $this->addElement($submit);
        
        /*
        //select place type
         $selectPlaceType = new Zend_Form_Element_Select('select_place_type',  
                         array('disableLoadDefaultDecorators' => true,
                                'required' => true
                         )); 
         $selectPlaceType->addMultiOption('0','--'.t(716).'--')
         				->addMultiOption('1', 'City')
         				->addMultiOption('2', 'Place')
         				->addDecorators($this->_noDecorator);
         $this->addElement($selectPlaceType);
         */
         //City
        $city_id = new Zend_Form_Element_Select('city_id',array('disableLoadDefaultDecorators' => true,
                                                             'required' => false));
        $city_id->addMultiOption('0','--'.t(717).'--');
        $table = new place_Model_City();
        $results = $table->fetchAll();
        $rets = $results->toArray();
        
        $topCities = Zend_Registry::get('configuration')->topCities->toArray();
        $city_id->addMultiOptions($topCities)
		        ->setAttrib('disable', array('1a'));

        foreach ($rets as $c) {
//        	if (!array_key_exists($c['city_id'], $topCities)) //only add non-top cities
	            $city_id->addMultiOption($c['city_id'], $c['city_name']);
        }
    	$city_id->addDecorators($this->_noDecorator);
        $this->addElement($city_id);
        
        //District
        $district_id = new Zend_Form_Element_Select('district_id',array('disableLoadDefaultDecorators' => true,
                                                                 'required' => true));
        $district_id->addMultiOption('0','--'.t(718).'--')
                    ->setRegisterInArrayValidator(false)	
                    ->addDecorators($this->_noDecorator);
 
        $this->addElement($district_id);
        
        //departure address
		$departure_city_id = new Zend_Form_Element_Select('departure_city_id',array('disableLoadDefaultDecorators' => true,
                                                             'required' => false));
        $departure_city_id->addMultiOption('0','--'.t(717).'--');
        $table = new place_Model_City();
        $results = $table->fetchAll();
        $rets = $results->toArray();
        
        $topCities = Zend_Registry::get('configuration')->topCities->toArray();
        $departure_city_id->addMultiOptions($topCities)
		        ->setAttrib('disable', array('1a'));

        foreach ($rets as $c) {
	            $departure_city_id->addMultiOption($c['city_id'], $c['city_name']);
        }
    	$departure_city_id->addDecorators($this->_noDecorator);
        $this->addElement($departure_city_id);
        
        //District
        $departure_district_id = new Zend_Form_Element_Select('departure_district_id',array('disableLoadDefaultDecorators' => true,
                                                                 'required' => true));
        $departure_district_id->addMultiOption('0','--'.t(718).'--')
                    ->setRegisterInArrayValidator(false)	
                    ->addDecorators($this->_noDecorator);
 
        $this->addElement($departure_district_id);
		        
        
        // Category
 		$category = new Zend_Form_Element_Select('category',  
 											  array('disableLoadDefaultDecorators' => true,
									          ));
		$mCategory = new category_Model_Category();
		$mCategory->setTables('place');
        $category->addMultiOption('0','--'.t(113).'--');
        foreach ($mCategory->getRootType('Root_Place') as $c) {
        	$c = (array)$c;
        	if($c['id'] != 89)
        	{
	            $category->addMultiOption($c['id'], $c['name']);
	        	$category->addDecorators($this->_noDecorator);
        	}
        }
        $this->addElement($category);
        
        //rtype
        $rtype = new Zend_Form_Element_Select('rtype',  array('disableLoadDefaultDecorators' => true,
                                                                 'required' => false));
		$rtype->addMultiOption('0','--'.t(112).'--');                                                                 
		$rtype->setRegisterInArrayValidator(false)
			  ->addDecorators($this->_noDecorator);
        $this->addElement($rtype);
    }
    
    public function populateForm($formName, $data)
    {
    	$mTleg = new Model_Tleg();
    	$data = $mTleg->transformToFormArray($data);
        //Populate district options
        $fData = unserialize($data['metadata']);
    	$mDistrict = new place_Model_District();
        if(isset($fData['arrival_city_id']))
        { 
        	$cityId = $fData['arrival_city_id'];
	    	if ($cityId != 0)
		    {
			    $districts = $mDistrict->getDistricts($cityId);
				foreach ($districts as $d) 
	          	    $this->district_id->addMultiOption($d->district_id, $d->district_name);
		    }
        }
        if(isset($fData['departure_city_id']))
        {
        	$cityId = $fData['departure_city_id'];
	    	if ($cityId != 0)
		    {
			    $districts = $mDistrict->getDistricts($cityId);
				foreach ($districts as $d) 
	          	    $this->departure_district_id->addMultiOption($d->district_id, $d->district_name);
		    }
        }
        
        $this->populate($data);
    }
}
?>